﻿
@{
    ViewBag.Title = "订单信息";
    var cus = ViewBag.Customer as CubicFWNet.Data.Models.Customer;
    var opList = ViewBag.CusProList as IEnumerable<CubicFWNet.Data.ViewModal.ProductView>;
}



<section class="content-header">
    <h1>
        订单信息
    </h1>
    <ol class="breadcrumb">
        <li><a href="javascript:void(0);"><i class="fa fa-dashboard"></i>@Resources.Global.PageTitle</a></li>
        <li><a href="@Url.Action("Index","Home")">首页</a></li>
        <li class="active">订单信息</li>
    </ol>
</section>

<section class="content">
    <form class="form-horizontal" method="post" id="formMain">
        <div class="box box-solid">
            <div class="box-body">
                <div class="row">
                    <input type="hidden" name="CustomerID" id="CustomerID" value="@cus.Id" />
                    <input type="hidden" name="ProData" id="ProData" />
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="CusName" class="col-sm-3 control-label">客户名称</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="CusName" name="CusName" value="@cus.Name" readonly />
                        </div>
                    </div>

                    <div class="form-group col-md-6 col-sm-12" style="display:none">
                        <label for="InitPrice" class="col-sm-3 control-label">订单总价</label>
                        <div class="col-sm-9">
                            <input type="number" class="form-control" id="Price" name="Price" value="@ViewBag.Price" readonly />
                        </div>
                    </div>

                    <div class="form-group col-md-6 col-sm-12">
                        <label for="Number" class="col-sm-3 control-label">内部订单号</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="Number" name="Number" value="@ViewBag.Number" readonly />
                        </div>
                    </div>

                    <div class="form-group col-md-6 col-sm-12">
                        <label for="Number" class="col-sm-3 control-label">客户订单号</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="CusNumber" name="CusNumber" />
                        </div>
                    </div>

                    <div class="form-group col-md-6 col-sm-12">
                        <label for="Delivery" class="col-sm-3 control-label">发货方式</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="Delivery" name="Delivery" />
                        </div>
                    </div>

                    <div class="form-group col-md-6 col-sm-12" style="display:none">
                        <label for="Channel" class="col-sm-3 control-label">订货途径</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="Channel" name="Channel" />
                        </div>
                    </div>

                    <div class="form-group col-md-6 col-sm-12">
                        <label for="SendTime" class="col-sm-3 control-label">希望交货期</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control Timedatepicker" id="SendTime" name="SendTime" />
                        </div>
                    </div>

                    <div class="form-group col-md-6 col-sm-12">
                        <label for="Number" class="col-sm-3 control-label">选择工序</label>
                        <div class="col-sm-9">
                            <label class="radio-inline"><input type="radio" name="IsWork" value="1" checked />生产分配</label>&nbsp;&nbsp;&nbsp;
                            <label class="radio-inline"><input type="radio" name="IsWork" value="2" />转库存</label>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="Effect" class="col-sm-3 control-label">选择产品</label>
                        <div class="col-sm-9 ">
                            <span class="btn btn-primary" onclick="ShowProList()">选择产品</span>
                        </div>
                    </div>
                </div>

                <div id="divProList">
                    @*@foreach (var item in opList)
                        {
                            <div class="row panelLayout" id="@item.Id">
                                <input type="text"  class="OPID" style="display:none" value="@item.OrderProductID" />

                                <div class="form-group col-md-6 col-sm-12">
                                    <label for="CusName" class="col-sm-3 control-label">产品名称</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control" value="@item.Name" readonly />
                                    </div>
                                </div>

                                <div class="form-group col-md-6 col-sm-12">
                                    <label for="CusName" class="col-sm-3 control-label">产品数量</label>
                                    <div class="col-sm-9">
                                        <input type="number" class="form-control Amount" value="@item.Amount" min="0" onblur="GetOrderPrice()" />
                                    </div>
                                </div>

                                <div class="form-group col-md-6 col-sm-12">
                                    <label for="CusName" class="col-sm-3 control-label">参考价</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control" value="@item.Price" readonly />
                                    </div>
                                </div>

                                <div class="form-group col-md-6 col-sm-12">
                                    <label for="CusName" class="col-sm-3 control-label">最终价</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control" value="@item.FinalPrice" readonly />
                                    </div>
                                </div>

                            </div>
                        }*@
                </div>
                <div class="row">
                    <div class="form-group col-md-6 col-sm-12">
                        <label for="Memo" class="col-sm-3 control-label">备注</label>
                        <div class="col-sm-9">
                            <textarea rows="3" class="form-control" id="Memo" name="Memo"></textarea>
                        </div>
                    </div>
                </div>
                <div class="row">

                </div>
            </div>
            <div class="box-footer">
                <div class="box-footer">
                    <span class="btn btn-primary " onclick="FormSub()" id="btnOk">确 定</span>&nbsp;&nbsp;&nbsp;&nbsp;
                    <a class="btn btn-primary" href="@Url.Action("ApplyList","Order")">返 回</a>&nbsp;&nbsp;&nbsp;&nbsp;
                    <label class="danger">@ViewBag.Info</label>
                </div>
            </div>
        </div>
    </form>
</section>

<!--选择产品-->
<div class="modal" id="proList" style="display: none">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">选择产品</h4>
            </div>
            <div class="modal-body">
                <div id="cusProList">
                    <div class="input-group">
                        <span class="input-group-btn">
                            <span class="btn btn-default btn-flat" onclick="Search()"> 搜 索 </span>
                        </span>
                        <input type="text" class="form-control" id="SearchName" name="SearchName" placeholder="请输入名称" onkeyup="Search()" />
                    </div><br />

                    @if (opList != null)
                    {
                        foreach (var item in opList)
                        {
                            <span class="btn btn-default productItem" data-proID="@item.ProductID" data-sel="0" data-price="@item.FinalPrice" data-cname="@item.ProductByName" data-opID="@item.OrderProductID" style="margin:8px">@item.Name</span>
                        }
                    }
                </div>
            </div>
            <div class="modal-footer">
                <a role="button" class="btn btn-default pull-left" data-dismiss="modal">关闭</a>
                <a role="button" class="btn btn-primary" data-dismiss="modal">完成</a>
            </div>
        </div>
    </div>
</div>


@section Scripts {
    <script>
        //日期控件
        $("#SendTime").datepicker({
            autoclose: true,
            language: 'zh', //汉化
            format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
            minView: "month"//选择日期后，不会再跳转去选择时分秒
        })

        //计算订单总价
        GetOrderPrice();

        //显示产品弹窗
        function ShowProList() {
            $("#proList").modal();
        }
        //点击选择产品
        $(".productItem").click(function () {
            var name = $(this).html();
            var id = $(this).attr("data-proID");
            var opid = $(this).attr("data-opID");
            var price = $(this).attr("data-price");
            var isSel = $(this).attr("data-sel");

            var cName = $(this).attr("data-cName");
            if (isSel == "1") {
                $("#" + id).remove();
                $(this).removeClass("btn-primary");
                $(this).addClass("btn-default");
                $(this).attr("data-sel", "0");
            }
            else {
                var html = "";
                html += "<div class=\"row panelLayout\" id=\"" + id + "\">";

                html += "<input type=\"hidden\"  class=\"OPID\" value=\"" + opid + "\" />";

                html += "<div class=\"form-group col-md-6 col-sm-12\">";
                html += "<label  class=\"col-sm-3 control-label\">产品名称</label>";
                html += "<div class=\"col-sm-9 \"><span class=\"form-control linkLayout\" onclick=\"ShowProModal('" + id + "')\">" + name + "</span></div>";
                html += "</div>";

                html += "<div class=\"form-group col-md-6 col-sm-12\">";
                html += "<label  class=\"col-sm-3 control-label\">客户产品名称</label>";
                html += "<div class=\"col-sm-9 \"><span class=\"form-control txtLayout\">" + cName + "</span></div>";
                html += "</div>";

                html += "<div class=\"form-group col-md-6 col-sm-12\">";
                html += "<label  class=\"col-sm-3 control-label\">购买数量</label>";
                html += "<div class=\"col-sm-9 \"><input type=\"number\" class=\"form-control Amount\" value=\"0\"  min=\"0\" onblur=\"GetOrderPrice()\"  /></div>";
                html += "</div>";

                html += "<div class=\"form-group col-md-6 col-sm-12\">";
                html += "<label  class=\"col-sm-3 control-label\">价格</label>";
                html += "<div class=\"col-sm-9 \"><input type=\"number\" class=\"form-control proPrice\"  value=\"" + price + "\"  readonly /></div>";
                html += "</div>";

                html += "</div>";

                $("#divProList").append(html);
                $(this).addClass("btn-primary");
                $(this).removeClass("btn-default");
                $(this).attr("data-sel", "1");
            }
        });

        //搜索产品
        function Search() {
            var name = $("#SearchName").val().trim();

            var list = $("#cusProList .productItem");
            var num = list.size();

            for (var i = 0; i < num; i++) {
                var sName = $("#cusProList .productItem").eq(i).html();
                if (sName.indexOf(name) >= 0) {
                    $("#cusProList .productItem").eq(i).show();
                }
                else {
                    $("#cusProList .productItem").eq(i).hide();
                }
            }
        }

        //提交数据
        function FormSub() {

            //验证订单号
            var number = $("#CusNumber").val().trim();
            if (number == "") {
                $.modalAlert("请输入客户订单号", "warning");
                return false;
            }

            var sendTime = $("#SendTime").val().trim();
            if (sendTime == "") {
                $.modalAlert("请输入交货期", "warning");
                return false;
            }

            //获取产品列表
            var proList = $("#divProList .panelLayout");

            var prodata = [];
            var num = proList.size();

            if (num <= 0) {
                $.modalAlert("请选择产品！", "warning");
                return false;
            }

            for (i = 0; i < num; i++) {
                var id = proList.eq(i).attr("id");
                var opid = $(".OPID").eq(i).val();
                var amount = $(".Amount").eq(i).val();
                if (amount == "" || amount <= 0) {
                    $.modalAlert("请输入正确产品数量！", "warning");
                    return false;
                }

                prodata.push({ OPID: opid, ProductID: id, Amount: amount });
            }

            $("#ProData").val(JSON.stringify(prodata));

            $("#btnOk").attr('disabled', "true")
            $("#formMain").submit();
        }

        //弹窗显示产品信息
        function ShowProModal(id) {
            var options = {
                id: null,
                title: '产品信息',
                width: "100%",
                height: "100%",
                url: "../Product/Info?PID=" + id,
                shade: 0.3,
                btn: ['返回'],
                btnclass: ['btn btn-danger'],
                callBack: null
            }
            $.modalOpen(options);
        }

        //获取订单总价
        function GetOrderPrice() {
            //获取产品列表
            var proList = $("#divProList .panelLayout");

            var num = proList.size();
            var total = 0;

            for (i = 0; i < num; i++) {
                var id = proList.eq(i).attr("id");
                var opid = $(".OPID").eq(i).val();
                var amount = $(".Amount").eq(i).val();
                var price = $(".Price").eq(i).val();
                if (amount == "" || amount < 0 || amount >= 10000000) {
                    $.modalAlert("请输入正确产品数量！", "warning");
                    return false;
                }

                total += (amount * price);
                $("#Price").val(total);
            }
        }

    </script>
}